//
//  ITDrawShapeViewController.m
//  InduceTool
//
//  Created by 魏延龙 on 2020/9/29.
//  Copyright © 2020 魏延龙. All rights reserved.
//

#import "ITDrawShapeViewController.h"

typedef enum : NSUInteger {
    SHAPETYPE_RECT,
    SHAPETYPE_TRIANDLE,
    SHAPETYPE_ARROW,
    SHAPETYPE_MOVE,
    SHAPETYPE_SIZE
} SHAPETYPE;

@interface ITDrawShapeViewController ()
{
    CGFloat _startAngle; // 开始的角度
}

@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@property (nonatomic, strong) UIBezierPath *bezPath;

@property (nonatomic, strong) CAShapeLayer *groupLayer;
@property (nonatomic, strong) UIBezierPath *bezPathGroup;

@property (nonatomic, strong) UISegmentedControl *segmentControl;

@property CGPoint firstPoint;
@property CGPoint lastPoint;
@property CGPoint endPoint;

@property SHAPETYPE shapeType;

@property BOOL canChangeSize;

@end

@implementation ITDrawShapeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    [self initSegmentControl];
    [self initShape];
    [self initGroupShape];
    self.bezPath = [UIBezierPath bezierPath];
    self.bezPathGroup = [UIBezierPath bezierPath];
}

- (void)initShape {
    self.shapeLayer = [CAShapeLayer layer];
    self.shapeLayer.fillColor = [UIColor clearColor].CGColor;
    self.shapeLayer.strokeColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:self.shapeLayer];
    self.shapeType = SHAPETYPE_RECT;
}

-(void)initGroupShape {
    self.groupLayer = [CAShapeLayer layer];
    self.groupLayer.fillColor = [UIColor clearColor].CGColor;
    self.groupLayer.strokeColor = [UIColor blueColor].CGColor;
    [self.view.layer addSublayer:self.groupLayer];
}

- (void)initSegmentControl {
    self.segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"矩形", @"三角形", @"移动", @"椭圆",@"箭头",@"大小"]];
    self.segmentControl.frame = CGRectMake(0, 88, [UIScreen mainScreen].bounds.size.width, 44);
    [self.segmentControl addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    [self.segmentControl setSelectedSegmentIndex:0];
    [self.view addSubview:self.segmentControl];
    self.segmentControl.selectedSegmentIndex = 0;
}

- (void)segAction:(UISegmentedControl *)control {
    switch (control.selectedSegmentIndex) {
        case 0:
            self.shapeType = SHAPETYPE_RECT;
            break;
        case 1:
            self.shapeType = SHAPETYPE_TRIANDLE;
            break;
        case 2:
            self.shapeType = SHAPETYPE_MOVE;
            break;
        case 3:
            [self drawCircleStart];
            break;
        case 4:
            self.shapeType = SHAPETYPE_ARROW;
            break;
        case 5:
            self.shapeType = SHAPETYPE_SIZE;
            break;
        default:
            break;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.shapeType != SHAPETYPE_MOVE && self.shapeType != SHAPETYPE_SIZE) {
        self.firstPoint = [touches.allObjects.firstObject locationInView:self.view];
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint current = [touches.allObjects.firstObject locationInView:self.view];

    switch (self.shapeType) {
        case SHAPETYPE_RECT:
            [self.bezPath removeAllPoints];
            [self drawRectStartPoint:self.firstPoint endPoint:current];
            self.shapeLayer.path = self.bezPath.CGPath;
            self.groupLayer.path = self.bezPathGroup.CGPath;
            break;
        case SHAPETYPE_TRIANDLE:
            [self.bezPath removeAllPoints];
            [self drawTriangleStartPoint:self.firstPoint endPoint:current];
            self.shapeLayer.path = self.bezPath.CGPath;
            break;
        case SHAPETYPE_MOVE:
            if (!CGPointEqualToPoint(self.lastPoint, CGPointZero) && [self.bezPath containsPoint:current]) {
                [self.bezPath removeAllPoints];
                CGFloat dexX = current.x - self.lastPoint.x;
                CGFloat dexY = current.y - self.lastPoint.y;
                self.firstPoint = CGPointMake(self.firstPoint.x + dexX, self.firstPoint.y + dexY);
                self.endPoint = CGPointMake(self.endPoint.x + dexX, self.endPoint.y + dexY);
                [self drawRectStartPoint:self.firstPoint endPoint:self.endPoint];
                self.shapeLayer.path = self.bezPath.CGPath;
                self.groupLayer.path = self.bezPathGroup.CGPath;
            }
            break;
        case SHAPETYPE_SIZE:
            {
                if (!CGPointEqualToPoint(self.lastPoint, CGPointZero) && [self.bezPathGroup containsPoint:current]) {
                    self.canChangeSize = YES;
                }
                if (self.canChangeSize) {
                    [self.bezPath removeAllPoints];
                    self.endPoint = CGPointMake(current.x, self.endPoint.y);
                    [self drawRectStartPoint:self.firstPoint endPoint:self.endPoint];
                    self.shapeLayer.path = self.bezPath.CGPath;
                    self.groupLayer.path = self.bezPathGroup.CGPath;
                }
            }
            break;
        case SHAPETYPE_ARROW:
            {
                [self.bezPath removeAllPoints];
                [self drawArrowStartPoint:self.firstPoint endPoint:current];
                self.shapeLayer.path = self.bezPath.CGPath;
            }
            break;

        default:
            break;
    }
    self.lastPoint = current;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.shapeType != SHAPETYPE_MOVE) {
        CGPoint current = [touches.allObjects.firstObject locationInView:self.view];
        self.endPoint = current;
    }
    self.lastPoint = CGPointZero;
    self.canChangeSize = NO;
}

// 画矩形
- (void)drawRectStartPoint:(CGPoint)point1 endPoint:(CGPoint)point2 {
    [self.bezPath moveToPoint:point1];
    [self.bezPath addLineToPoint:CGPointMake(point2.x, point1.y)];
    [self.bezPath addLineToPoint:point2];
    [self.bezPath addLineToPoint:CGPointMake(point1.x, point2.y)];
    [self.bezPath closePath];
    
    
    
    [self.bezPathGroup removeAllPoints];
    CGFloat xs = point2.x;
    CGFloat ys = point1.y + (point2.y-point1.y)/2;
    UIBezierPath *circlePath1 = [UIBezierPath bezierPathWithArcCenter:point1 radius:10 startAngle:0 endAngle:2 * M_PI clockwise:NO];
    
    UIBezierPath *circlePath2 = [UIBezierPath bezierPathWithArcCenter:(CGPoint){xs,ys} radius:10 startAngle:0 endAngle:2 * M_PI clockwise:NO];
    
    UIBezierPath *circlePath3 = [UIBezierPath bezierPathWithArcCenter:point2 radius:10 startAngle:0 endAngle:2 * M_PI clockwise:NO];
    [self.bezPathGroup appendPath:circlePath1];
    [self.bezPathGroup appendPath:circlePath2];
    [self.bezPathGroup appendPath:circlePath3];
    
}

// 画三角
- (void)drawTriangleStartPoint:(CGPoint)point1 endPoint:(CGPoint)point2 {
    [self.bezPath moveToPoint:CGPointMake(point1.x + (point2.x - point1.x) / 2, point1.y)];
    [self.bezPath addLineToPoint:point2];
    [self.bezPath addLineToPoint:CGPointMake(point1.x, point2.y)];
    [self.bezPath closePath];
}

// 画箭头
-(void)drawArrowStartPoint:(CGPoint)point1 endPoint:(CGPoint)point2 {
    
    CGFloat x1 = point1.x;
    CGFloat y1 = point1.y;
    CGFloat x2 = point2.x;
    CGFloat y2 = point2.y;
    
    CGFloat y_l = y2 - y1;
    CGFloat x_l = x2 - x1;
    CGFloat r_value = atanf(y_l/x_l);
    
    CGFloat s_l = sqrt(x_l * x_l + y_l * y_l) - 10;
    // 三角形 底边中点坐标
    CGFloat correction = x2 > x1 ? 1 : -1;
    CGFloat x = correction * s_l * cosf(r_value) + x1;
    CGFloat y = correction * s_l * sinf(r_value) + y1;
    
    // 三角形下方坐标
    CGFloat x_b1 = x - 1 * sinf(r_value);
    CGFloat y_b1 = y + 1 * cosf(r_value);
    
    CGFloat x_b = x - 5 * sinf(r_value);
    CGFloat y_b = y + 5 * cosf(r_value);
    
    // 三角形上方坐标
    CGFloat x_a1 = x + 1 * sinf(r_value);
    CGFloat y_a1 = y - 1 * cosf(r_value);
    
    CGFloat x_a = x + 5 * sinf(r_value);
    CGFloat y_a = y - 5 * cosf(r_value);
    
    [self.bezPath moveToPoint:point1];
    [self.bezPath addLineToPoint:CGPointMake(x_b1, y_b1)];
    [self.bezPath addLineToPoint:CGPointMake(x_b, y_b)];
    [self.bezPath addLineToPoint:point2];
    [self.bezPath addLineToPoint:CGPointMake(x_a, y_a)];
    [self.bezPath addLineToPoint:CGPointMake(x_a1, y_a1)];
    [self.bezPath closePath];
}

- (void)drawCircleStart {
    self.bezPath = [UIBezierPath bezierPathWithArcCenter:(CGPoint){200,300} radius:20 startAngle:0 endAngle:M_PI clockwise:NO];
    self.shapeLayer.path = self.bezPath.CGPath;
    [self drawLineAnimation:self.shapeLayer];
}

- (void)drawLineAnimation:(CALayer*)layer {
    CABasicAnimation *bas = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    bas.duration = 1;
    bas.fromValue = [NSNumber numberWithInteger:0];
    bas.toValue = [NSNumber numberWithInteger:1];
    [layer addAnimation:bas forKey:@"key"];
    
}

-(void)drawGroup {
    CGFloat x = self.endPoint.x;
    CGFloat y = self.firstPoint.y + (self.endPoint.y-self.firstPoint.y)/2;
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:(CGPoint){x,y} radius:50 startAngle:0 endAngle:2 * M_PI clockwise:NO];
}


@end
